package com.tensquare.spit.controller;

import com.tensquare.entity.*;
import com.tensquare.entity.StatusCode;
import com.tensquare.spit.pojo.Spit;
import com.tensquare.spit.service.SpitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by WF on 2020/12/16 17:39
 */
@RestController
@RequestMapping("spit")
public class SpitController {
    @Autowired
    private SpitService spitService;
    //1.查询所有
    @GetMapping
    public Result findAll(){
        //1.1)查询所有
        List<Spit> spits = spitService.findAll();
        //1.2)返回
        return new Result(true, StatusCode.OK,"查询列表成功！",spits);
    }

    //2.查询单个吐槽
    @GetMapping("{spitId}")
    public Result findById(@PathVariable String spitId){
        //2.1)根据id查询吐槽
        Spit spit = spitService.findById(spitId);
        //2.2)返回
        return new Result(true,StatusCode.OK,"查询单个吐槽成功！",spit);
    }

    //3.添加吐槽
    @PostMapping
    public Result add(@RequestBody Spit spit){
        //3.1)开始添加吐槽
        spitService.add(spit);
        //3.2)返回
        return new Result(true,StatusCode.OK,"添加吐槽成功！");
    }

    //4.修改吐槽
    @PutMapping("{spitId}")
    public Result update(@PathVariable String spitId,@RequestBody Spit spit){
        //4.1)绑定主键与吐槽对象
        spit.set_id(spitId);
        //4.2)修改
        spitService.update(spit);
        //4.3)返回
        return new Result(true,StatusCode.OK,"修改吐槽成功！");
    }

    //5.删除吐槽
    @DeleteMapping("{spitId}")
    public Result delete(@PathVariable String spitId){

        //5.1)删除吐槽
        spitService.delete(spitId);
        //5.2)返回
        return new Result(true,StatusCode.OK,"删除吐槽成功！");
    }

    //6.根据上级id查询吐槽列表
    @GetMapping("comment/{parentid}/{page}/{size}")
    public Result comment(@PathVariable String parentid,@PathVariable Integer page,@PathVariable Integer size){
        //6.1)根据上级id查询吐槽列表
        PageResult<Spit> spitPageResult = spitService.comment(parentid,page,size);
        //6.2)返回
        return new Result(true,StatusCode.OK,"查询吐槽列表成功！",spitPageResult);
    }

    //7.处理吐槽点赞
    @PutMapping("thumbup/{spitId}")
    public Result thumbup(@PathVariable String spitId){

        //7.1)处理点赞
        boolean b = spitService.thumbup2(spitId);
        if(!b){
            return new Result(false,StatusCode.ERROR,"不能重复点赞！");
        }
        //7.2）返回
        return new Result(true,StatusCode.OK,"点赞成功！");
    }
}
